package com.cloudptt.api.models;



import java.util.HashMap;
import java.util.Map;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;

import android.content.Context;

/**
 * SIP消息扩展部分信息
 * @author TP
 *
 */
public class ExtStrInfo 
{
	public final static int Login_Suc = 200;
	public final static int Login_Fail = 1;
	public final static int Login_AuthTimeout = 2;
	public final static int Login_ReLogin = 3;
	
	public final static int AccountType_IMSI = 1;
	public final static int AccountType_IMEI = 2;
	
	public final static String DefauleValue = "";
	public final static String Key_IMSI = "IMIS";
	public final static String Key_PUID = "PUID";
	public final static String Key_CallingPUID = "CallingPUID";
	public final static String Key_CalledPUID = "CalledPUID";
	public final static String Key_Mdn = "MDN";
	public final static String Key_CountryCode = "CountryCode";
	public final static String Key_Name = "Name";
	public final static String Key_GID = "GID";
	public final static String Key_Cause = "Cause";
	public final static String Key_CallType = "CallMode";
	public final static String Key_MediaType = "MediaType";
	public final static String Key_ServType = "ServType";
	public final static String Key_InactiveTime = "InactiveTime";
	public final static String Key_MemberSpeakTime = "MemberSpeakTime";
	public final static String Key_UEPrivilege = "UEPrivilege";
	public final static String Key_AccountType = "AccountType";
	
	public final static String Title_HttpAuth = "http_auth";
	public final static String Title_CallErr = "call_err";
	
	public final static String Title_pttreg = "pttreg";
	public final static String Title_pttUnReg = "pttunreg";
	public final static String Title_pttcallreq = "pttcallreq";
	public final static String Title_pttfloorreq = "pttfloorreq";
	public final static String Title_pttfloorrelreq = "pttfloorrelreq";

	public final static String Title_pttregresp = "pttregresp";
	public final static String Title_pttcallresp = "pttcallresp";

	public final static String Title_pttcallconn = "pttcallconn";
	public final static String Title_pttcallsetupconf = "pttcallsetupconf";
	public final static String Title_pttmediaconn = "pttmediaconn";


	public final static String Title_pttfloorrelresp = "pttfloorrelresp";
	public final static String Title_pttfloorresp = "pttfloorresp";
	public final static String Title_CallDescDeny = "call_desc_deny";
	
	private String title = null;
	private Map<String,String> valueMap = null;
	
//	private String THIS_FILE = "ExtStrInfo";

	public ExtStrInfo(String extStr)
	{
		String[] strArr = extStr.split(";");
		valueMap = new HashMap<String,String>();
		for(int i = 0; i < strArr.length; i++)
		{
			String value = strArr[i];
			if(!value.contains("="))
			{
				this.title = strArr[i].trim();
			}
			else
			{
				String[] valueArr = value.split("=");
				if(valueArr.length == 2)
				{
					if(valueArr[0] != null
							&& valueArr[1] != null)
					{
						valueMap.put(valueArr[0].trim(), valueArr[1].trim());
					}
				}
			}
		}
	}

	public String getTitle() {
		return title;
	}

	public String getValue(String key)
	{
		if(key == null
				|| key.equals(""))
		{
			return DefauleValue;
		}
		
		String value = valueMap.get(key);
		if( value != null ) {
			return value;
		}
		
		return DefauleValue;
	}


	public boolean isLogin()
	{
		if(title != null
				&& (title.equals(Title_pttreg)
						|| title.equals(Title_pttUnReg)))
		{
			return true;
		}
		return false;
	}
	public int getLoginResult()
	{
		String strValue = getValue(Key_Cause);
		if(strValue != null)
		{
			int cause = CheckData.parseInt(strValue);//-1为未知错误
			if(cause == 200)
			{
				return Login_Suc;
			}
//			if(cause == 3)
//			{
//				return Login_ReLogin;
//			}
			else if(cause == 7)
			{
				return Login_AuthTimeout;
			}
		}
		return Login_Fail;
	}
	
	public int getCause()
	{
		String str = getValue(Key_Cause);
		int cause = CheckData.parseInt(str);
		return cause;
	}
	public String getUserName()
	{
		String name = getValue(Key_Name);
//		name = Common.unicode2String(name);
		return name;
	}

//	public ErrInfo getErrInfo(Context ctx)
//	{
//		if(title == null
//				|| title.length() == 0)
//		{
//			return null;
//		}
//
//		String strValue = getValue(Key_Cause);
//		if(strValue == null)
//		{
//			return null;
//		}
//
//		int cause = CheckData.parseInt(strValue);//-1为未知错误
//
//		return new ErrInfo(title,cause, ctx);
//	}
//
	public String getErrDesc(Context ctx)
	{
		String strValue = getValue(Key_Cause);
		if(strValue == null)
		{
			return "";
		}
		int cause = CheckData.parseInt(strValue);//-1为未知错误
		if( -1 != cause)
		{
			ErrInfo errInfo = new ErrInfo(title,cause);
			if( errInfo != null )
			{
				return errInfo.getErrDesc();
			}
		}
		return "";
	}

	public static String getErrDesc(Context ctx,String title,int code)
	{
		if( -1 != code)
		{
			ErrInfo errInfo = new ErrInfo(title,code);
			if( errInfo != null )
			{
				return errInfo.getErrDesc();
			}
		}
		return "";
	}

	public int getCallType()
	{
		String strValue =  getValue(Key_CallType);
		Log.d("ExtStrInfo", "getCallType strValue = " + strValue);
		return CheckData.parseInt(strValue);
	}
	
	public boolean isReLogin()
	{
		String strValue =  getValue(Key_Cause);
		Log.d("ExtStrInfo", "isReLogin strValue = " + strValue);
		if(strValue != null
				&& strValue.equals("101"))
		{
			return true;
		}
		return false;
	}
	
	public String toString()
	{
		if(valueMap == null)
		{
			return "valueMap = null";
		}
		return valueMap.toString();
	}
}
